<?php
class ControllerDelimanKybao extends Controller
{
	private $error = array();
	
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
		$this->data['list_button'] = array();
		foreach($listchek as $method)
		{
			$this->data['list_button'][$method] = $this->user->hasPermission($this->getRoute(), $method);
		}
		
	 	$this->load->model("deliman/kybao");
		$this->load->model("deliman/loaibao");
		$this->load->model("common/control");
		$this->data['dialog'] = $this->request->get['dialog'];
   	}
	
	public function index()
	{
		$this->getList();
	}
	
	public function insert()
	{
    	$this->getForm();
	}
	
	public function update()
	{
		$this->data['haspass'] = false;
		$this->data['readonly'] = 'readonly="readonly"';
		$this->data['class'] = 'readonly';
		$this->getForm();	
  	}
	
	//Ham lay thong tin ky bao
	public function getdkkybao()
	{
		$this->load->model("deliman/kybao");	
		$this->load->model("common/control");
		
		$loaibaoid = $this->request->get['loaibaoid'];
		$select =  $this->request->get['select'];
		
		if($loaibaoid != "")
		{
			$kybaos = $this->model_deliman_kybao->getList(" AND loaibaoid='".$loaibaoid);
		}
		else
		{
			$kybaos = $this->model_deliman_kybao->getList();
		}
		
		$this->data['output'] = $this->model_common_control->getDataCombobox($kybaos,"tenkybao","kybaoid",$select);
		
		$this->template='common/output.tpl';
		$this->render();
	}
	
	//Ham lay thong tin ky bao load vao combo box
	public function getcbkybao()
	{	
		$this->load->model("common/control");
		
		$loaibaoid = $this->request->get['loaibaoid'];
		$select =  $this->request->get['select'];
		
		$kybaos = $this->model_deliman_kybao->getList(" AND loaibaoid='".$loaibaoid."'");
		$this->data['output'] = $this->model_common_control->getDataCombobox($kybaos,"tenkybao","kybaoid",$select);
		
		$this->template='common/output.tpl';
		$this->render();
	}
	
	//Ham lay thong tin so ky bao max load vao form them ky bao
	public function getsokybao()
	{		
		$loaibaoid = $this->request->get['loaibaoid'];
				
		$sokybao=$this->model_deliman_kybao->getItemMax($loaibaoid);
		
		$this->data['output'] = json_encode($sokybao);
		$this->template='common/output.tpl';
		$this->render();
	}
	
	public function delete() 
	{
		$listkybaoid=$this->request->post['delete'];
		if(count($listkybaoid))
		{
			foreach($listkybaoid as $kybaoid)
			{				
				$this->model_deliman_kybao->delete($kybaoid);	
			}
			$this->data['output'] = "true";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function getList() 
	{
		
		$data = $this->request->get;
		//dieu kien tim kiem
		if(trim($data['tungay'])!='')
		{
			$where .= " AND ngayphathanh >= '".$this->date->formatViewDate($data['tungay'])."'";
		}
		if(trim($data['denngay'])!='')
		{
			$where .= " AND ngayphathanh <= '".$this->date->formatViewDate($data['denngay'])."'";
		}
		if(trim($data['loaibaoid'])!='')
		{
			$where .= " AND loaibaoid = '".$data['loaibaoid']."'";
		}
		if(trim($data['kybaoid'])!='')
		{			
			//Danh sach ky bao ma khach hang da dat
			$where .= " AND kybaoid = '".$data['kybaoid']."'";			
		}
		$this->data['insert'] = $this->url->http('deliman/kybao/insert');
		$this->data['delete'] = $this->url->http('deliman/kybao/delete');	
		//Lay so ky bao	
		//print_r($this->data['sokybao']=$this->model_deliman_kybao->getItemMax("6"));
		
		$this->load->model("deliman/loaibao");
		$this->data['loaibaos'] = $this->model_deliman_loaibao->getList();
		//load cb loai bao
		$this->data['loaibao'] = $this->model_common_control->getDataCombobox($this->data['loaibaos'], "tenloaibao", "loaibaoid", $select);
		
				
		//Page
		$rows = $this->model_deliman_kybao->getList($where." ORDER BY ngayphathanh DESC");
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		
		//khai bao bien thong bao khi không có dữ liệu trong hệ thống
		$this->data['thongbao'] = 0;
		
		$this->data['datas'] = array();
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['thongbao']++;
			
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['link_edit'] = $this->url->http('deliman/kybao/update&kybaoid='.$this->data['datas'][$i]['kybaoid']);
			$this->data['datas'][$i]['text_edit'] = "Sửa";
			$loaibao = $this->model_deliman_loaibao->getItem($this->data['datas'][$i]['loaibaoid']);
			$this->data['datas'][$i]['tenloaibao'] = $loaibao['tenloaibao'];
			
		}
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/kybao_list.tpl";
		if($this->request->get['dialog'] == true)
		{
			$this->layout="layout/dialog";
		}
		else
		{
			$this->layout="layout/deliman";
		}
		
		
		$this->render();
	}
	
	
	private function getForm()
	{
		//cb loai bao
		$this->load->model('deliman/loaibao');
		$this->data['loaibaos'] = $this->model_deliman_loaibao->getList();
		
		
		if ((isset($this->request->get['kybaoid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_kybao->getItem($this->request->get['kybaoid']);
    	}
		
		$this->id='content';
		$this->template='deliman/kybao_form.tpl';
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	public function save()
	{
		$data = $this->request->post;
		if($this->validateForm($data))
		{
			$this->load->model("deliman/loaibao");
			$loaibao = $this->model_deliman_loaibao->getItem($data['loaibaoid']);
			$data['tenloaibao'] = $loaibao['tenloaibao'];
			$data['ngayphathanh'] = $this->date->formatViewDate($data['ngayphathanh']);
			$data['giain']=$this->string->toNumber($this->request->post['giain']);
			$data['chiphikhac']=$this->string->toNumber($this->request->post['chiphikhac']);
			$data['giaban']=$this->string->toNumber($this->request->post['giaban']);
			$data['tonggiagoc']=$this->string->toNumber($this->request->post['tonggiagoc']);
			$data['soluongin']=$this->string->toNumber($this->request->post['soluongin']);
			$data['sotrang']=$this->string->toNumber($this->request->post['sotrang']);
			if($data['kybaoid']=="")
			{				
				$this->model_deliman_kybao->insert($data);							
			}
			else
			{				
				$this->model_deliman_kybao->update($data);	
			}
			
			$this->data['output'] = "true";
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		if(trim($data['loaibaoid']) == "")
		{
			$this->error['loaibaoid'] = "Bạn chưa nhập tên loại báo";
		}
		
		if (trim($data['tenkybao']) == "") 
		{
      		$this->error['tenkybao'] = "Bạn chưa nhập tên kỳ báo";
    	}
		
		if (trim($data['sokybao']) == "0") 
		{
      		$this->error['sokybao'] = "Bạn chưa nhập số kỳ báo";
    	}
		
		if (trim($data['ngayphathanh']) == "") 
		{
      		$this->error['ngayphathanh'] = "Bạn chưa nhập ngày phát hành";
    	}
		
		if (trim($data['giaban']) == "0") 
		{
      		$this->error['giaban'] = "Bạn chưa nhập giá bán";
    	}
		
		if (trim($data['giain']) == "0") 
		{
      		$this->error['giain'] = "Bạn chưa nhập giá in";
    	}
		
		if (trim($data['soluongin']) == "0") 
		{
      		$this->error['soluongin'] = "Bạn chưa nhập số lượng in";
    	}
		
		if (trim($data['sotrang']) == "0") 
		{
      		$this->error['sotrang'] = "Bạn chưa nhập số trang";
    	}
	
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	public function getKyBao()
	{
		$col = $this->request->get['col'];
		$val = $this->request->get['val'];
		$operator = $this->request->get['operator'];
		if($operator == "")
			$operator = "equal";
		
		$where = "";
		switch($operator)
		{
			case "equal":
				$where = " AND ".$col." = '".$val."'";
				break;
			case "like":
				$where = " AND ".$col." like '%".$val."%'";
				break;
			case "other":
				$where = " AND ".$col." <> '".$val."'";
				break;
			case "in":
				$where = " AND ".$col." in  (".$val.")";
				break;
			
		}
			
		
		$datas = $this->model_deliman_kybao->getList($where." ORDER BY ngayphathanh DESC");
		foreach($datas as $key => $item)
		{
			$loaibao = $this->model_deliman_loaibao->getItem($item['loaibaoid']);
			$datas[$key]['tenloaibao'] = $loaibao['tenloaibao'];
			$datas[$key]['ngayphathanh'] = $this->date->formatMySQLDate($item['ngayphathanh']);
		}
		
		
		$this->data['output'] = json_encode(array('kybaos' => $datas));
		$this->id="kybao";
		$this->template="common/output.tpl";
		$this->render();
	}
}
?>